package com.hwx.product.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @Desc: 商品微服务控制器
 * @Author: hwx
 * @Date: 2020/5/26 14:03
 * @Version: v1.0
 **/
@RestController
@RequestMapping(value = "/product")
public class ProductController {

	@Value("${server.port}")
	private String port;

	@Value("${spring.cloud.client.ip-address}")//spring cloud 自动的获取当前应用的ip地址
	private String ip;


	private static final Map<Integer, String> productMap = new HashMap();

	//模拟数据库商品
	static {
		productMap.put(1, "西红柿");
		productMap.put(2, "玩具车");
	}


	/**
	 * @desc: 根据id查询对应的商品
	 * @author: hwx
	 * @date: 2020/5/26 14:06
	 * @param: id -> 商品id
	 * @return: 详情数据
	 */
	@GetMapping(value = "/{id}")
	public JSONObject getProductById(@PathVariable Integer id) {
		JSONObject obj = new JSONObject();
		String product = productMap.get(id);
		obj.put("msg", "提供商品服务的路径为：" + ip + ":" + port);
		obj.put("product", product);
		return obj;
	}

}